package cn.iocoder.yudao.module.alipay.service.infoorganize;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.alipay.controller.admin.infoorganize.vo.*;
import cn.iocoder.yudao.module.alipay.dal.dataobject.infoorganize.InfoOrganizeDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.alipay.convert.infoorganize.InfoOrganizeConvert;
import cn.iocoder.yudao.module.alipay.dal.mysql.infoorganize.InfoOrganizeMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.alipay.enums.ErrorCodeConstants.*;

/**
 * 机构详情 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class InfoOrganizeServiceImpl implements InfoOrganizeService {

    @Resource
    private InfoOrganizeMapper infoOrganizeMapper;

    @Override
    public Long createInfoOrganize(InfoOrganizeCreateReqVO createReqVO) {
        // 插入
        InfoOrganizeDO infoOrganize = InfoOrganizeConvert.INSTANCE.convert(createReqVO);
        infoOrganizeMapper.insert(infoOrganize);
        // 返回
        return infoOrganize.getId();
    }

    @Override
    public void updateInfoOrganize(InfoOrganizeUpdateReqVO updateReqVO) {
        // 校验存在
        validateInfoOrganizeExists(updateReqVO.getId());
        // 更新
        InfoOrganizeDO updateObj = InfoOrganizeConvert.INSTANCE.convert(updateReqVO);
        infoOrganizeMapper.updateById(updateObj);
    }

    @Override
    public void deleteInfoOrganize(Long id) {
        // 校验存在
        validateInfoOrganizeExists(id);
        // 删除
        infoOrganizeMapper.deleteById(id);
    }

    private void validateInfoOrganizeExists(Long id) {
        if (infoOrganizeMapper.selectById(id) == null) {
            throw exception(INFO_ORGANIZE_NOT_EXISTS);
        }
    }

    @Override
    public InfoOrganizeDO getInfoOrganize(Long id) {
        return infoOrganizeMapper.selectById(id);
    }

    @Override
    public List<InfoOrganizeDO> getInfoOrganizeList(Collection<Long> ids) {
        return infoOrganizeMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<InfoOrganizeDO> getInfoOrganizePage(InfoOrganizePageReqVO pageReqVO) {
        return infoOrganizeMapper.selectPage(pageReqVO);
    }

    @Override
    public List<InfoOrganizeDO> getInfoOrganizeList(InfoOrganizeExportReqVO exportReqVO) {
        return infoOrganizeMapper.selectList(exportReqVO);
    }

    @Override
    public InfoOrganizeDO getByTenantId(Long tenantId) {
        return infoOrganizeMapper.getByTenantId(tenantId);
    }

}
